package com.benmei.weike.web;

import com.benmei.weike.common.Constants;
import com.benmei.weike.dto.IncourseAddGoodsDto;
import com.benmei.weike.dto.otherplatform.LoginDto;
import com.benmei.weike.entity.Student;
import com.benmei.weike.exception.ClientException;
import com.benmei.weike.exception.ServerException;
import com.benmei.weike.service.PaymentService;
import com.benmei.weike.service.StudentService;
import com.benmei.weike.util.IPUtil;
import com.benmei.weike.util.JsonUtil;
import com.nativetalk.base.RetInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Peter on 2018/4/14.
 * 对接外部系统的接口(专门为InCourse写的接口)
 */
@RestController
public class OtherPlatformController {
    public static final Logger logger = LoggerFactory.getLogger(OtherPlatformController.class);

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/api/incourse/account/login", method = {RequestMethod.POST}, consumes = "application/json")
    public RetInfo addAppointment(@RequestBody LoginDto loginDto) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(loginDto));

            if (StringUtils.isBlank(loginDto.getUid())) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter uid  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }

            Student student=studentService.loginOrRegiest(loginDto);

            RetInfo retInfo = RetInfo.getSuccessInfo("成功");
            retInfo.setObj(student);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (ClientException e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getClientErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }  catch (ServerException e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    // incourse 学生支付成功后调用此接口为用户添加课程
    @RequestMapping(value = "/api/incourse/payment/addGoods", method = {RequestMethod.POST}, consumes = "application/json")
    public RetInfo addGoods(@RequestBody IncourseAddGoodsDto dto, HttpServletRequest request) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(dto));

            if (StringUtils.isBlank(dto.getUid())) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter uid  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (StringUtils.isBlank(dto.getOrderNo())) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter orderNo  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (dto.getGoodsId() == null) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter goodsId  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (dto.getCreateDate()==null) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter createDate  is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }

            paymentService.incourseAddGoods(dto, IPUtil.getIp(request));

            RetInfo retInfo = RetInfo.getSuccessInfo("成功");
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (ClientException e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getClientErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }  catch (ServerException e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo(e.getMessage());
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);
            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }


}
